#include "UserLoginCmd.h"

UserLoginCmd::UserLoginCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
    m_loginTime.clear();
    m_account.clear();
    m_password.clear();
}


Package UserLoginCmd::toPackage()
{
    Package package;
    QByteArray body;

    quint16 strLength;

    strLength = m_loginTime.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_loginTime);

    strLength = m_account.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_account);

    strLength = m_password.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_password);

    body = encrypt(body,KeyType::AES);

    package.setId(m_cmdId);
    package.setFunction(FunctionCode);
    package.setBody(body);

    return package;
}

void UserLoginCmd::unPackage(Package package)
{
    m_cmdId = package.id();

    QByteArray body;
    quint16 strLength,index = 0;

    body = decrypt(package.body(),KeyType::AES);

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_loginTime = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_account = body.mid(index,strLength);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_password = body.mid(index,strLength);
}
QByteArray UserLoginCmd::loginTime() const
{
    return m_loginTime;
}

void UserLoginCmd::setLoginTime(const QByteArray &LoginTime)
{
    m_loginTime = LoginTime;
}
QString UserLoginCmd::account() const
{
    return m_account;
}

void UserLoginCmd::setAccount(const QString &account)
{
    m_account = account;
}
QString UserLoginCmd::password() const
{
    return m_password;
}

void UserLoginCmd::setPassword(const QString &password)
{
    m_password = password;
}



